# -*- coding: utf-8 -*-
import sys,androidTools,Const,Util

def getAChannelIpa(channel_id ,path_apk_channel):
	'''
	修改包渠道号
	'''
	#对渠道号文件进行修改
	Util.changeChannel(Const.PATH_IPA_FILE_SOURCEID, channel_id)

	#将解压出来文件夹再压缩成IPA文件
	Util.zip_dir(Const.BASE_NAME_IPADIR,path_apk_channel,)

if __name__ == '__main__':
	argLen = len(sys.argv)
	if(argLen < 5):
		print ('参数个数错误！')
		exit(1)
	VERSION = sys.argv[1]
	DIR_IPA_BASE = sys.argv[2]
	DIR_IPA_CHANNEL = sys.argv[3]
	CHANNELS = sys.argv[4]

	arry_cannels = CHANNELS.split(',')

	path_ipa_base = DIR_IPA_BASE + '/' +Const.NAME_FILE_PREFIX + VERSION + '.ipa'

	dir_ipa_channel = DIR_IPA_CHANNEL+'/'+ VERSION +'/'

	#创建该版本对应的渠道包文件夹
	Util.makedirs(dir_ipa_channel)

	#将母包解压出来
	Util.unzip_file(path_ipa_base,Const.BASE_NAME_IPADIR)

	#生成每个渠道包
	for channel_id in arry_cannels:
		path_ipa_channel = dir_ipa_channel+Const.NAME_FILE_PREFIX+VERSION+'_'+channel_id+'.ipa'
		getAChannelIpa(channel_id,path_ipa_channel)

	#清理操作
	Util.DelFolder(Const.BASE_NAME_IPADIR)
